using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WozLib.UI.Ink.SketchProperties;

namespace WozLib.UI.Controls
{
  /// <summary>
  /// Interaction logic for ThicknessSelector.xaml
  /// </summary>

  public partial class ThicknessSelector : UserControl
  {
    public static readonly DependencyProperty SelectedThicknessProperty =
            DependencyProperty.Register("SelectedThickness", typeof(Thickness), typeof(ThicknessSelector),
      new UIPropertyMetadata(new Thickness(1.1), new PropertyChangedCallback(SelectedThickness_Changed)));

    public Thickness SelectedThickness
    {
      get { return (Thickness)GetValue(SelectedThicknessProperty); }
      set { SetValue(SelectedThicknessProperty, value); }
    }

    public static readonly DependencyProperty SelectedThicknessLeftProperty =
            DependencyProperty.Register("SelectedThicknessLeft", typeof(double), typeof(ThicknessSelector),
      new UIPropertyMetadata(1.0, new PropertyChangedCallback(SelectedThicknessLeft_Changed)));

    public double SelectedThicknessLeft
    {
      get { return (double)GetValue(SelectedThicknessLeftProperty); }
      set { SetValue(SelectedThicknessLeftProperty, value); }
    }

    public static readonly DependencyProperty AllowZeroProperty =
            DependencyProperty.Register("AllowZero", typeof(bool), typeof(ThicknessSelector),
      new UIPropertyMetadata(true, new PropertyChangedCallback(AllowZeroProperty_Changed)));

    public bool AllowZero
    {
      get { return (bool)GetValue(AllowZeroProperty); }
      set { SetValue(AllowZeroProperty, value); }
    }

    public static readonly DependencyProperty CascadingPropertyProperty =
            DependencyProperty.Register("CascadingProperty", typeof(CascadingProperty), typeof(ThicknessSelector));

    public CascadingProperty CascadingProperty
    {
      get { return (CascadingProperty)GetValue(CascadingPropertyProperty); }
      set { SetValue(CascadingPropertyProperty, value); }
    }


    public ThicknessSelector()
    {
      InitializeComponent();
      this.Loaded += new RoutedEventHandler(ThicknessSelector_Loaded);
    }

    void ThicknessSelector_Loaded(object sender, RoutedEventArgs e)
    {
      _ZeroRadio.Checked += new RoutedEventHandler(_ZeroRadio_Checked);
      _OneRadio.Checked += new RoutedEventHandler(_OneRadio_Checked);
      _TwoRadio.Checked += new RoutedEventHandler(_TwoRadio_Checked);
      _FiveRadio.Checked += new RoutedEventHandler(_FiveRadio_Checked);

      _SmallSlider.PreviewMouseDown += new MouseButtonEventHandler(_Slider_PreviewMouseDown);
      _BigSlider.PreviewMouseDown += new MouseButtonEventHandler(_Slider_PreviewMouseDown);

      _SmallSlider.PreviewMouseUp += new MouseButtonEventHandler(_Slider_PreviewMouseUp);
      _BigSlider.PreviewMouseUp += new MouseButtonEventHandler(_Slider_PreviewMouseUp);
    }

    void _Slider_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
      if (CascadingProperty != null)
      {
        CascadingProperty.InContinuousChange = true;
      }
    }

    void _Slider_PreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
      if (CascadingProperty != null)
      {
        CascadingProperty.InContinuousChange = false;
      }
    }

    void _FiveRadio_Checked(object sender, RoutedEventArgs e)
    {
      if (!_suspendSet)
      {
        this.SelectedThicknessLeft = 5.0;
      }
    }

    void _TwoRadio_Checked(object sender, RoutedEventArgs e)
    {
      if (!_suspendSet)
      {
        this.SelectedThicknessLeft = 2.0;
      }
    }

    void _OneRadio_Checked(object sender, RoutedEventArgs e)
    {
      if (!_suspendSet)
      {
        this.SelectedThicknessLeft = 1.0;
      }
    }

    void _ZeroRadio_Checked(object sender, RoutedEventArgs e)
    {
      if (!_suspendSet)
      {
        this.SelectedThicknessLeft = 0.0;
      }
    }

    private static void AllowZeroProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    { }

    private bool _suspendSet = false;
    private static void SelectedThicknessLeft_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

      if (d is ThicknessSelector && e.NewValue is double)
      {
        ThicknessSelector thicknessSelector = d as ThicknessSelector;
        double thicknessLeft = (double)e.NewValue;
        if (!thicknessSelector._suspendSet)
        {
          // assume uniform thickness
          thicknessSelector.SelectedThickness = new Thickness(thicknessLeft);
        }
      }
    }
    private static void SelectedThickness_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

      if (d is ThicknessSelector && e.NewValue is Thickness)
      {
        ThicknessSelector thicknessSelector = d as ThicknessSelector;
        Thickness thickness = (Thickness)e.NewValue;
        if (!thicknessSelector._suspendSet)
        {
          thicknessSelector._suspendSet = true;

          thicknessSelector.SelectedThicknessLeft = thickness.Left;

          // Assume thickness is uniform
          double thicknessLeft = thickness.Left;
          // Zero
          if (thicknessLeft < 0.05 && thicknessSelector.AllowZero)
          {
            thicknessSelector._ZeroRadio.IsChecked = true;
          }
          // One
          else if (thicknessLeft < 2.00 && thicknessLeft > 0.95)
          {
            thicknessSelector._OneRadio.IsChecked = true;
          }
          // Two
          else if (thicknessLeft < 3.00 && thicknessLeft > 1.00)
          {
            thicknessSelector._TwoRadio.IsChecked = true;
          }
          // Five
          else if (thicknessLeft < 6.00 && thicknessLeft > 4.00)
          {
            thicknessSelector._FiveRadio.IsChecked = true;
          }
          else
          {
            thicknessSelector._OtherRadio.IsChecked = true;
          }

          thicknessSelector._suspendSet = false;
        }
      }
    }

  }
}